<?php
/**
 * 推送,信息
 */
namespace App\Http\Controllers;

use App\Service\MsgService;
use App\Validation\ListValidation;

class MsgController extends BaseController
{

	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new ListValidation,$this->request->getJsonRawBody());
		$raw->member_id = $this->user->member_id;
		$data = MsgService::getList($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->logo_image = $value->logo_image ? $this->getFullUrl($value->logo_image, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->go_url = $this->config->gourl . $this->config->urls->msg . $value->id;
		}
		$count = MsgService::count($raw);
		return  $this->echoJson(['total'=>$count,'list'=>$d]);
	}
	
	//详情
    public function detailAction()
    {
		$this->getUser();
		$id = $this->dispatcher->getParam('id');
		$raw = new \stdClass;
		$raw->id = (int)$id;
		$raw->member_id = $this->user->member_id;
		$data = MsgService::getInfo($raw);
		// var_dump($data->go_url);die;
		

		//消息已读
        MsgService::read($id);
        $arr = current($data) ? $data[0] : new \stdClass();
        
        if (count((array)$arr))
        	$arr->go_url = $this->config->gourl . $this->config->urls->msg . $arr->id;     	

		return  $this->echoJson($arr);
    }
}